package com.wangjiyuan.giftshuo.ui.fragment.home;

import com.wangjiyuan.giftshuo.bean.home.column.HomeBean;

import rx.Subscriber;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by wjy on 2016/11/18.
 */

public class RecyclerPresenter implements RecyclerContract.RecyclerPresenter {

    private RecyclerContract.RecyclerModel model;
    private RecyclerContract.RecyclerView view;
    private Subscription subscribe;

    public RecyclerPresenter(RecyclerContract.RecyclerView view) {
        model = new RecyclerModel();
        this.view = view;

    }

    @Override
    public void loadDateBean(int id) {
        subscribe = model.loadHomeBean(id)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new Subscriber<HomeBean>() {
                    @Override
                    public void onCompleted() {
                        subscribe.unsubscribe();
                    }

                    @Override
                    public void onError(Throwable e) {
                        subscribe.unsubscribe();
                    }

                    @Override
                    public void onNext(HomeBean homeBean) {
                        view.loadDate(homeBean.getData().getItems());
                    }
                });
    }

    @Override
    public void loadNextDateBean(int id, int offset) {
        subscribe = model.loadNextHomeBean(id, offset)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new Subscriber<HomeBean>() {
                    @Override
                    public void onCompleted() {
                        subscribe.unsubscribe();
                    }

                    @Override
                    public void onError(Throwable e) {
                        subscribe.unsubscribe();
                    }

                    @Override
                    public void onNext(HomeBean homeBean) {
                        view.loadNextDate(homeBean.getData().getItems());
                    }
                });
    }
}
